package com.atguigu.spzx.user.controller;

import com.atguigu.spzx.model.dto.user.UserAddressDto;
import com.atguigu.spzx.model.pojo.user.UserAddress;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.user.service.UserAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user/userAddress")
@Tag(name = "用户地址接口")
public class UserAddressController {
    @Autowired
    private UserAddressService userAddressService;

    @GetMapping("auth/findUserAddressList")
    @Operation(summary = "获取用户地址列表")
    public Result<List<UserAddress>> findUserAddressList(){
        return Result.ok(userAddressService.list());
    }


    @GetMapping("auth/getUserAddress/{id}")
    @Operation(summary = "根据地址id获取地址信息")
    public Result<UserAddress> getUserAddress(@PathVariable("id")Integer id){
        return Result.ok(userAddressService.getById(id));
    }

    @PostMapping("/auth/save")
    @Operation(summary = "新增用户地址")
    public Result saveUserAddress(@RequestBody UserAddress userAddress){
        userAddressService.saveUserAddress(userAddress);
        return Result.ok();
    }

    @PutMapping("/auth/updateById")
    @Operation(summary = "修改用户地址")
    public Result updateUserAddress(@RequestBody UserAddressDto userAddressDto){
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userAddressDto.getUserId());
        userAddress.setName(userAddressDto.getName());
        userAddress.setPhone(userAddressDto.getPhone());
        userAddress.setTagName(userAddressDto.getTagName());
        userAddress.setProvinceCode(userAddressDto.getProvinceCode());
        userAddress.setCityCode(userAddressDto.getCityCode());
        userAddress.setDistrictCode(userAddressDto.getDistrictCode());
        userAddress.setAddress(userAddressDto.getAddress());
        userAddress.setIsDefault(userAddressDto.getIsDefault());
        userAddress.setId(Long.valueOf(userAddressDto.getId()));
        userAddress.setIsDeleted(0);
        userAddressService.updateUserAddress(userAddress);
        return Result.ok();
    }

    @DeleteMapping("/auth/removeById/{id}")
    @Operation(summary = "删除用户地址")
    public Result deleteUserAddress(@PathVariable("id")Long id){
        userAddressService.removeById(id);
        return Result.ok();
    }

}
